<?php
namespace Tbf\Bundle\TbfBundle\DependencyInjection;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\Finder\Finder;
use Tbf\Component\Classes\ClassFunction;
use Tbf\Component\GiveName\GiveNameFunction;
use Tbf\Component\Path\PathFunction;

/**
 * 定义一个目录的服务
 */
class DirectoryServiceDefiner {
    protected $suffix_name;
    protected $class_base;
    protected $di_base;
    protected $base_dir;
    protected $definition_template;
    static public function create(){
        return new static();
    }

    public function load(ContainerBuilder $container){
        $class_map = ClassFunction::getClassMapInPath($this->base_dir);
        foreach($class_map as $class_name=>$class_path){
            $meta_class = new \ReflectionClass($class_name);
            if ($meta_class->isAbstract()){
                continue;
            }
            if ($meta_class->isInterface()){
                continue;
            }
            $relative_path = PathFunction::getRelativePathTo($this->base_dir,$class_path);
            $ret = preg_match('/(.+)'.$this->suffix_name.'.php$/',$relative_path,$match);
            if (empty($ret)){
                continue;
            }
            $di_name = $this->di_base.str_replace('/','.',$match[1]);
            $di_name = $this->DiNameTranslate($di_name);
            $definition = clone $this->definition_template;
            $container->setDefinition($di_name,$definition)
                ->setClass($class_name);
        }
        $container->addResource(new DirectoryResource($this->base_dir));
    }

    protected  function DiNameTranslate($origin){
        $array = explode('.',$origin);
        foreach($array as $k1=>$v1){
            $array[$k1] = GiveNameFunction::to($v1,'GiveName','give_name');
        }
        return implode('.',$array);
    }

    /**
     * @param string $class_base
     * @return DirectoryServiceDefiner
     */
    public function setClassBase($class_base)
    {
        $this->class_base = $class_base;
        return $this;
    }

    /**
     * @param string $di_base
     * @return DirectoryServiceDefiner
     */
    public function setDiBase($di_base)
    {
        $this->di_base = $di_base;
        return $this;
    }

    /**
     * @param string $suffix_name
     * @return DirectoryServiceDefiner
     */
    public function setSuffixName($suffix_name)
    {
        $this->suffix_name = $suffix_name;
        return $this;
    }

    /**
     * @param mixed $base_dir
     */
    public function setBaseDir($base_dir)
    {
        $this->base_dir = realpath($base_dir);
        return $this;
    }

    /**
     * 使用di定义模板,仅修改定义class为搜索到对应class
     * @param Definition $definition_template
     */
    public function setDefinitionTemplate(Definition $definition_template)
    {
        $this->definition_template = $definition_template;
        return $this;
    }

}